package com.zhoug.mpchart.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.IMarker;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.zhoug.mpchart.MPChartConfig;
import com.zhoug.mpchart.formatters.MPAxisTextValueFormatter;
import com.zhoug.mpchart.ints.IMPChartPlaceholder;
import com.zhoug.mpchart.utils.MPChartPlaceholderHelper;
import com.zhoug.mpchart.utils.MPChartScrollHelper;

/**
 * @Author: zhoug
 * @Date: 2024-01-30
 * @Description:
 */
public class MPChartHelper {
    protected Chart<?> mChart;
    protected MPChartScrollHelper mChartScrollHelper;
    protected IMPChartPlaceholder mChartPlaceholder;
    protected boolean mPlaceholderEnable = true;
    protected int mAnimDirection= IMPChart.ANIM_DIRECTION_X;

    public MPChartHelper(Chart<?> chart, Context context, AttributeSet attrs, int defStyle) {
        this.mChart = chart;
        mChartScrollHelper = new MPChartScrollHelper(chart);
        mChartPlaceholder = new MPChartPlaceholderHelper(chart, context, attrs, defStyle);
        if (chart instanceof BarLineChartBase) {
            MPChartConfig.initChartConfig((BarLineChartBase<?>) chart);
        }else if(chart instanceof PieChart){
            MPChartConfig.initPieChartConfig((PieChart) chart);
        }else if(chart instanceof RadarChart){
            MPChartConfig.initRadarChartConfig((RadarChart) chart);
        }
    }

    /**
     * 占位图位置工具
     *
     * @param chartPlaceholder
     */
    public void setChartPlaceholder(IMPChartPlaceholder chartPlaceholder) {
        this.mChartPlaceholder = chartPlaceholder;
    }


    public void onLayout() {
        if (mChart != null) {
            mChartScrollHelper.findViewPager(mChart.getParent());
        }
    }

    public void onTouchEvent(MotionEvent event) {
        mChartScrollHelper.onTouchEvent(event);
    }


    /**
     * 暂无数据
     */
    public void empty() {
        if (mPlaceholderEnable && mChartPlaceholder != null) {
            mChartPlaceholder.empty();
        }
    }

    /**
     * 数据加载失败
     */
    public void error() {
        if (mPlaceholderEnable && mChartPlaceholder != null) {
            mChartPlaceholder.error();
        }
    }

    /**
     * 重置占位图
     */
    public void resetPlaceholder() {
        if (mPlaceholderEnable && mChartPlaceholder != null) {
            mChartPlaceholder.resetPlaceholder();
        }
    }

    /**
     * 在图表组件的onDraw(Canvas canvas)方法后调用
     *
     * @param canvas
     */
    public void drawPlaceImage(Canvas canvas) {
        if (mPlaceholderEnable && mChartPlaceholder != null) {
            mChartPlaceholder.drawPlaceImage(canvas);
        }
    }

    /**
     * 占位图是否启用
     *
     * @param enable
     * @return
     */
    public void setPlaceholderEnable(boolean enable) {
        this.mPlaceholderEnable = enable;
    }

    /**
     * x轴开始的最小值
     *
     * @return
     */
    public int getStartXValue() {
        MPAxisTextValueFormatter axisTextValueFormatter = getAxisTextValueFormatter();
        if (axisTextValueFormatter != null) {
            return axisTextValueFormatter.getMinValue();
        }
        return 0;
    }

    /**
     * 获取x轴数据格式化器
     *
     * @return
     */
    public MPAxisTextValueFormatter getAxisTextValueFormatter() {
        if (mChart != null) {
            ValueFormatter valueFormatter = mChart.getXAxis().getValueFormatter();
            if (valueFormatter instanceof MPAxisTextValueFormatter) {
                return ((MPAxisTextValueFormatter) valueFormatter);
            }
        }
        return null;
    }


    /**
     * 不缩放,x,y的scale重置为1
     *
     * @param maxScaleX x的最大缩放倍数 0不限制
     * @param maxScaleY y的最大缩放倍数 0不限制
     */
    public void zoomNotScale(float maxScaleX, float maxScaleY) {
        if (mChart instanceof BarLineChartBase) {
            ViewPortHandler viewPortHandler = mChart.getViewPortHandler();
            viewPortHandler.setMaximumScaleX(maxScaleX);
            viewPortHandler.setMaximumScaleY(maxScaleY);
            float scaleX = 1.0f / viewPortHandler.getScaleX();
            float scaleY = 1.0f / viewPortHandler.getScaleY();
            if (scaleX != 1 || scaleY != 1) {
                ((BarLineChartBase<?>) mChart).zoom(scaleX, scaleY, 0, 0);
            }
        }
    }

    /**
     * 不缩放,x,y的scale重置为1
     */
    public void zoomNotScale() {
        zoomNotScale(0, 0);
    }

    /***
     * {@link IMPChart#ANIM_DIRECTION_X, IMPChart#ANIM_DIRECTION_Y, IMPChart#ANIM_DIRECTION_XY}
     * @param anim
     */
    public  void setAnimateDirection(int anim) {
        this.mAnimDirection = anim;
    }

    /**
     * {@link IMPChart#ANIM_DIRECTION_X, IMPChart#ANIM_DIRECTION_Y, IMPChart#ANIM_DIRECTION_XY}
     */
    public int getAnimateDirection() {
        return mAnimDirection;
    }

    public void animate(){
        if(mChart!=null){
            int animateDirection = getAnimateDirection();
            if (animateDirection == IMPChart.ANIM_DIRECTION_X) {
                mChart.animateX(MPChartConfig.ANIMATE_DURING);
            } else if (animateDirection ==  IMPChart.ANIM_DIRECTION_Y) {
                mChart.animateY(MPChartConfig.ANIMATE_DURING);
            } else if (animateDirection ==  IMPChart.ANIM_DIRECTION_XY) {
                mChart.animateXY(MPChartConfig.ANIMATE_DURING, MPChartConfig.ANIMATE_DURING);
            }
        }
    }

    public void setMarker(IMarker marker) {
        if(marker instanceof MarkerView){
            ((MarkerView) marker).setChartView(mChart);
        }
    }
}
